﻿using UnityEngine;
using System.Collections;
using Cmd;
using DG.Tweening;
using GameData;
using UnityEngine.UI;

public class SaleUI : MonoBehaviour
{

    public GameObject btn_calcle;
    public GameObject btn_call;
    public GameObject btn_next;
    public GameObject btn_start;
    public GameObject btn_end;
    public GameObject btn_setNum;

    public GameObject btn_back;
    public InputField NumField;
    public Text CurNumber;
    public Text CurWindow;
    public Text TipText;
    public int CurWindowsIndex;
    public NumberInfo CurNumberInfo;
    public Text CurdNText;
    public bool ischangScene = true;


	// Use this for initialization
	void Start () {

        EventTriggerListener.Get(btn_calcle).onClick = OnCancle;
        EventTriggerListener.Get(btn_call).onClick = OnCall;
        EventTriggerListener.Get(btn_next).onClick = OnNext;
        EventTriggerListener.Get(btn_start).onClick = OnStart;
        EventTriggerListener.Get(btn_end).onClick = OnEnd;
        EventTriggerListener.Get(btn_setNum).onClick = OnSetNum;
	    if (btn_back!= null)
	    {
            EventTriggerListener.Get(btn_back).onClick = OnBack;

        }

        SetWindowNumSend winsend = new SetWindowNumSend();
        NextNumber nx = new NextNumber();
	    nx.windowsid = CurWindowsIndex;

        winsend.Send(nx, windowNum);

        Messenger<NumAllInfo>.AddListener("SC统计数据", OnNumberInfo);

	    OnNumberInfo(GlobalVO.GetInstance().NumAllInfo);


	}

    void OnNumberInfo(NumAllInfo numallInfo)
    {
        if (numallInfo==null)
        {
            return;
        }
        //
       
    }


    public void RemoveListener()
    {
        Messenger<NumAllInfo>.RemoveListener("SC统计数据", OnNumberInfo);

    }

    void windowNum(NumberInfo numberInfo)
    {

        CurNumberInfo = numberInfo;
        OnNextNumber(CurNumberInfo);

    }

    /// <summary>
    /// 放弃办理
    /// </summary>
    /// <param name="obj"></param>
    void OnCancle(GameObject obj)
    {
        if (CurNumberInfo==null)
        {
            return;
        }

        CancleNumberSend csend = new CancleNumberSend();
        NextNumber ne = new NextNumber();
        ne.windowsid = CurWindowsIndex;
        ne.value = CurNumberInfo.value;
        ne.groupname = CurNumberInfo.@group;
        CurNumberInfo.state = (int) NumberState.Cancle;
        csend.Send(ne, CompleteNum);
        TipText.text = "放弃办理";
    }

    void OnCall(GameObject obj)
    {
        if (CurNumberInfo== null)
        {
            //下一位
            OnNext(null);
            return;
        }
        else if (CurNumberInfo.state ==(int)NumberState.Cancle || CurNumberInfo.state == (int)NumberState.Complete)
        {
            //下一位
            OnNext(null);
            return;
        }

        CallNumberSend callNumberSend = new CallNumberSend();
        CallNumber callNumber = new CallNumber();
        callNumber.windowsid = this.CurWindowsIndex;
        callNumber.numberInfo = CurNumberInfo;
        callNumberSend.Send(callNumber, OnCallRes);


    }

    void OnCallRes(CallNumber  cal)
    {
        
    }

    void OnNext(GameObject obj)
    {

        //
        DueNumberSend dueSend = new DueNumberSend();
        NextNumber nex = new NextNumber();
        nex.windowsid = CurWindowsIndex;
        dueSend.Send(nex, OnNextNumber);
        TipText.text = "办理中";
    }

    void OnNextNumber(NumberInfo numberinfo)
    {
        Debug.Log("===判断处理："+ numberinfo);

        Debug.Log("处理=====+"+ numberinfo.value);
        if (numberinfo.value==-1)
        {
            CurNumberInfo = null;
            CurNumber.text = "办理号码：";

            return;
        }
        CurNumberInfo = numberinfo;
        //设置
        CurNumber.text = "办理号码："+ UtilTool.getNumbertoStr(numberinfo.value) ;
        CurdNText.text = "当前排队人数：" + numberinfo.PrintCount;
    }

    /// <summary>
    /// 完成办理
    /// </summary>
    /// <param name="obj"></param>
    void OnStart(GameObject obj)
    {
        if (CurNumberInfo == null)
        {
            return;
        }

        CompleteNumberSend csend = new CompleteNumberSend();
        NextNumber ne = new NextNumber();
        ne.windowsid = CurWindowsIndex;
        ne.value = CurNumberInfo.value;
        ne.groupname = CurNumberInfo.@group;
        CurNumberInfo.state = (int)NumberState.Complete;
        csend.Send(ne, CompleteNum);
        TipText.text = "完成办理";
    }

    void CompleteNum(NumberInfo num)
    {
        //完成办理


    }

    void OnEnd(GameObject obj)
    {

    }
    void OnSetNum(GameObject obj)
    {
        if (string.IsNullOrEmpty(NumField.text))
        {
            return;
        }
        CurWindowsIndex= int.Parse(NumField.text);
        CurWindow.text = CurWindowsIndex.ToString() + "号窗口";
        //
        CurNumberInfo = null;
        CurNumber.text = "办理号码：";

        //设置窗口
        SetWindowNumSend winsend = new SetWindowNumSend();
        NextNumber nx = new NextNumber();
        nx.windowsid = CurWindowsIndex;
        winsend.Send(nx, windowNum);
    }


    void OnBack(GameObject obj)
    {
        if (ischangScene)
        {
            Messenger<NumAllInfo>.RemoveListener("SC统计数据", OnNumberInfo);

            Application.LoadLevel("Main");

        }
        else
        {
            transform.localScale = new Vector3(1, 1, 1);
            Tweener tw = transform.DOScale(new Vector3(0, 0, 0), 0.3f).SetEase(Ease.OutBack);
            tw.OnComplete(() => gameObject.SetActive(false));
        }


    }

    // Update is called once per frame
    void Update () {
	
	}
}
